using System;
using System.Collections.Generic;
using System.Xml;
using SSP.Api.Interfaces;

namespace SSP.Api
{
    /// <summary>
    /// 
    /// </summary>
    public class IPTC : IDeserializer
    {

        /// <summary>
        /// Gets or sets the Byline.
        /// </summary>
        /// <value>The Byline.</value>
        public string Byline { get; set; }

        /// <summary>
        /// Gets or sets the BylineTitle.
        /// </summary>
        /// <value>The BylineTitle.</value>
        public string BylineTitle { get; set; }

        /// <summary>
        /// Gets or sets the caption.
        /// </summary>
        /// <value>The caption.</value>
        public string Caption { get; set; }

        /// <summary>
        /// Gets or sets the category.
        /// </summary>
        /// <value>The category.</value>
        public string Category { get; set; }

        /// <summary>
        /// Gets or sets the city.
        /// </summary>
        /// <value>The city.</value>
        public string City { get; set; }

        /// <summary>
        /// Gets or sets the contact.
        /// </summary>
        /// <value>The contact.</value>
        public string Contact { get; set; }

        /// <summary>
        /// Gets or sets the copyright.
        /// </summary>
        /// <value>The copyright.</value>
        public string Copyright { get; set; }

        /// <summary>
        /// Gets or sets the country.
        /// </summary>
        /// <value>The country.</value>
        public string Country { get; set; }

        /// <summary>
        /// Gets or sets the credit.
        /// </summary>
        /// <value>The credit.</value>
        public string Credit { get; set; }

        /// <summary>
        /// Gets or sets the headline.
        /// </summary>
        /// <value>The headline.</value>
        public string Headline { get; set; }

        /// <summary>
        /// Gets or sets the keywords.
        /// </summary>
        /// <value>The keywords.</value>
        public string Keywords { get; set; }

        /// <summary>
        /// Gets or sets the source.
        /// </summary>
        /// <value>The source.</value>
        public string Source { get; set; }

        /// <summary>
        /// Gets or sets the state.
        /// </summary>
        /// <value>The state.</value>
        public string State { get; set; }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title { get; set; }

        #region IDeserializer Members

        /// <summary>
        /// Reads the XML.
        /// </summary>
        /// <param name="reader">The reader.</param>
        public void DeserializeFromReader(XmlReader reader)
        {

            // Start to use the reader.
            reader.Read();

            // Read all elements
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                switch (reader.Name)
                {
                    case "byline":
                        Byline = reader.ReadElementContentAsString();
                        break;
                    case "byline_title":
                        BylineTitle = reader.ReadElementContentAsString();
                        break;
                    case "caption":
                        Caption = reader.ReadElementContentAsString();
                        break;
                    case "category":
                        Category = reader.ReadElementContentAsString();
                        break;
                    case "city":
                        City = reader.ReadElementContentAsString();
                        break;
                    case "contact":
                        Contact = reader.ReadElementContentAsString();
                        break;
                    case "copyright":
                        Copyright = reader.ReadElementContentAsString();
                        break;
                    case "country":
                        Country = reader.ReadElementContentAsString();
                        break;
                    case "credit":
                        Credit = reader.ReadElementContentAsString();
                        break;
                    case "headline":
                        Headline = reader.ReadElementContentAsString();
                        break;
                    case "keywords":
                        Keywords = reader.ReadElementContentAsString();
                        break;
                    case "source":
                        Source = reader.ReadElementContentAsString();
                        break;
                    case "state":
                        State = reader.ReadElementContentAsString();
                        break;
                    case "title":
                        Title = reader.ReadElementContentAsString();
                        break;
                    default:
                        System.Diagnostics.Trace.WriteLine("Unhandled xml element name '" + reader.Name + "'.");
                        _sDict[reader.Name] = reader.ReadElementContentAsString();
                        break;
                }
                reader.MoveToContent();
            }

            // Extremely important to read the node to its end.
            // next call of the reader methods will crash if not called.
            reader.ReadEndElement();

        }

        /// <summary>
        /// Gets or sets the <see cref="System.String"/> with the specified property name.
        /// </summary>
        /// <value></value>
        public string this[string propertyName]
        {
            get { return _sDict[propertyName]; }
            set { _sDict[propertyName] = value; }
        }

        private readonly Dictionary<string, string> _sDict = new Dictionary<string, string>();

        #endregion
    }
}